package com.example.lijuan.myapplication.UI.Activity;

import android.app.AlarmManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Base64;

import com.amap.api.location.LocationManagerProxy;
import com.example.lijuan.myapplication.Util.LocationController;

//import app.akexorcist.bluetotohspp.library.BluetoothSPP;

/**
 * Created by nilesh on 9/23/2015.
 */
public class DashboardApp extends Application {
    private static Context context;


    public static PowerManager getPowerManager() {
        return powerManager;
    }

    public static void setPowerManager(PowerManager powerManager) {
        DashboardApp.powerManager = powerManager;
    }

    public static LocationManagerProxy getLocationManager() {
        return locationManager;
    }

   public static void setLocationManager(LocationManagerProxy locationManager) {
        DashboardApp.locationManager = locationManager;
    }

    public static AlarmManager getAlarmManager() {
        return alarmManager;
    }

    public static void setAlarmManager(AlarmManager alarmManager) {
        DashboardApp.alarmManager = alarmManager;
    }

    public static SharedPreferences getLocalStorage() {
        return localStorage;
    }

    public static void setLocalStorage(SharedPreferences localStorage) {
        DashboardApp.localStorage = localStorage;
    }

    public static Context getContext() {
        return context;
    }

    public static void setContext(Context context) {
        DashboardApp.context = context;
    }

//    public static BluetoothSPP getBluetoothSPP() {
//        return bt;
//    }

    private static SharedPreferences localStorage;
    private static PowerManager powerManager;
   private static LocationManagerProxy locationManager;
    private static AlarmManager alarmManager;
//    private static BluetoothSPP bt;
    private static Boolean hasData = false;
    private static LocationController locationController;
    @Override
    public void onCreate() {
        super.onCreate();
        context = this.getApplicationContext();
        localStorage = PreferenceManager.getDefaultSharedPreferences(context);
        powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        locationManager = LocationManagerProxy.getInstance(context);
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//        bt = new BluetoothSPP(context);
        setupLocationController(context);
    }

    public static String encryptBASE64(byte[] key) {
        return Base64.encodeToString(key, Base64.DEFAULT);
    }

    public static byte[] decryptBASE64(String data) {
        return Base64.decode(data, Base64.DEFAULT);
    }

    public static Boolean getHasData(){
        return hasData;
    }

    public static void saveRealTimeData(byte[] data) {
        hasData = true;
        localStorage = DashboardApp.getLocalStorage();
        localStorage.edit().putString("RTDATA", encryptBASE64(data)).apply();
    }

    public static byte[] readRealTimeData() {
        localStorage = DashboardApp.getLocalStorage();
        return  decryptBASE64(localStorage.getString("RTDATA", ""));
    }

    public static LocationController getLocationController() {
        return locationController;
    }

    private void setupLocationController(Context context) {
        locationController = LocationController.getInstance();
        locationController.start(context);
    }
}
